package com.xneure.code.log;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.xneure.code.log.ILog.Level;
import com.xneure.utils.ObjectUtil;
import com.xneure.utils.StackUtil;
import com.xneure.utils.bean.Caller;

public abstract class LogWatcher{
	protected Set<Class<ILog>> logCls = null;
	protected Level level = Level.ALL;
	protected FileWriter fos;
	protected String[] regex = {".*"};

	public abstract void handle(ILog iLog,Level level,String log);

	public abstract void saveLog();

	public Caller[] getCallStack(String cls){
		return StackUtil.getCaller(cls);
	}

	public void setRegex(String regex){
		if(ObjectUtil.isNull(regex)){
			this.regex = null;
			return;
		}
		String[] tmp = regex.split(",");
		List<String> tem = new ArrayList<String>(tmp.length);
		for(String r:tmp){
			if(null != r && r.trim().length() > 0){
				tem.add(r);
			}
		}
		this.regex = tem.toArray(new String[tem.size()]);
	}

	/**
	 * 打印调试信息
	 * 
	 * @param log void
	 */
	protected void printLog(String log){
		try{
			LogManage.out.write(log);
		}catch(IOException e){
		}
	}

	/**
	 * 打印调试信息--错误
	 * 
	 * @param log void
	 */
	protected void printErrLog(String log){
		try{
			LogManage.err.write(log);
		}catch(IOException e){
		}
	}

	protected void printLog(Level level,String log){
		if(level == Level.ERROR){
			printErrLog(log);
		}else{
			printLog(log);
		}
	}

	public void monitorLog(Class<ILog> cls){
		if(logCls == null){
			logCls = new HashSet<Class<ILog>>();
		}
		logCls.add(cls);
	}

	public Set<Class<ILog>> getLogCls(){
		return logCls;
	}
}
